'use strict';
angular.module('app').controller('positionCtrl', ['$q', '$http', '$state', '$scope', 'cache', '$log',  function ($q, $http, $state, $scope, cache, $log) {
    $scope.isLogin = !!cache.get('name');
    $scope.message = $scope.isLogin?'投个简历':'去登录';
    function  getPosition() {
        var def = $q.defer();
        $http({
            method :'GET',//post,delete,put
            url: '/data/position.json',
            params: {
                id: $state.params.id
            }
        }).then(function successCallback(resp) {
            $scope.position = resp.data;
            if(resp.posted){
                $scope.message = '已投递';
            }
            def.resolve(resp.data);
        },function errCallback (err) {
            def.reject(err);
        });
        return def.promise;
    }
    function getCompany(id) {
        $http({
            method :'GET',
            url: '/data/company.json?id='+id
        }).then(function successCallback(resp) {
            $scope.company = resp.data;
        },function errCallback (err) {
        })
    }

    getPosition().then(function (obj) {
        getCompany(obj.companyId);
    });
    // //两个异步操作执行完成后，在进行另外的操作
    // $q.all([fun1(), fun2()]).then(function (result) {
    //
    // })
    //$timeout(function () {}, 0)//放到执行队列的最末端，并不是马上执行，单线程
    //$interval 和$timeout类似
    //$rootScope
    
    $scope.go = function () {
        if($scope.message!=='已投递'){
            if($scope.isLogin){
                $http.post('data/handle.json',{
                    id: $scope.position.id
                }).success(function (resp) {
                    $log.info(resp);
                    $scope.message = '已投递';
                })
            }else{
                $state.go('login');
            }
        }
    }
}]);